package com.heu.blood.match.controller;

import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.match.dto.req.BloodInformationQueryPageReqDTO;
import com.heu.blood.match.entity.BloodInformationEntity;
import com.heu.blood.match.service.BloodInformationService;
import com.heu.blood.match.vo.BloodInformationEntityVo;
import com.heu.blood.match.vo.BloodInformationIdHistoryVo;
import com.heu.blood.match.vo.InventoryQueryVo;
import com.heu.blood.storge.vo.BloodDeleteReturnVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.heu.blood.match.vo.BloodInventoryResultVo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 
 *
 * @author lyj
 * @email lyj@heu.com
 * @date 2023-06-16 00:23:13
 */
@Slf4j
@RestController
@RequestMapping("match/bloodinformation")
public class BloodInformationController {
    @Autowired
    private BloodInformationService bloodInformationService;

    /**
     * 从excel表批量导入血液信息
     * @param multipartFile
     * @return
     */
    @PostMapping("/import")
    @ControllerLogAnnotation(module = "血液信息", operate = "从excel表批量导入血液信息")
    public R importExcel(@RequestParam("file") MultipartFile multipartFile){
        bloodInformationService.importExcel(multipartFile);
        return R.ok("ok");
    }


    /**
     * 分页查询血液信息
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "血液信息", operate = "分页查询血液信息")
    public R list(@RequestBody BloodInformationQueryPageReqDTO requestParam){
        PageUtils page = bloodInformationService.queryPage(requestParam);
        return R.ok().put("page", page);
    }

    /**
     * 条件查询血液信息
     * @param bloodInformationEntityVo
     * @return
     */
    @RequestMapping("/query")
    @ControllerLogAnnotation(module = "血液信息", operate = "条件查询血液信息")
    public R query(@RequestBody BloodInformationEntityVo bloodInformationEntityVo){
        List<BloodInformationEntity> bloodInformationEntities = bloodInformationService.query(bloodInformationEntityVo);
        return R.ok().put("list", bloodInformationEntities);
    }


    /**
     * 根据血液id查询血液信息
     * @param bloodId
     * @return
     */
    @GetMapping("/queryByBloodId")
    @ControllerLogAnnotation(module = "血液信息", operate = "查询血液信息")
    public R queryByBloodId(String bloodId){
        BloodInformationEntity bloodInformationEntity = bloodInformationService.queryByBloodId(bloodId);
        return R.ok().put("data", bloodInformationEntity);
    }

    /**
     * 信息
     */
    @ControllerLogAnnotation(module = "血液信息", operate = "根据id查询血液信息")
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        BloodInformationEntity bloodInformation = bloodInformationService.getById(id);

        return R.ok().put("data", bloodInformation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "血液信息", operate = "新增血液信息")
    public R save(@RequestBody BloodInformationEntity bloodInformation){
        bloodInformationService.save(bloodInformation);

        return R.ok();
    }

    /**
     * 批量插入血液信息表
     */
    @RequestMapping("/saveBatchBloodInformation")
    public R saveBatchBloodInformation(@RequestBody List<BloodInformationEntity> bloodInformationEntityList){
        bloodInformationService.saveBatch(bloodInformationEntityList);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ControllerLogAnnotation(module = "血液信息", operate = "修改血液信息")
    public R update(@RequestBody BloodInformationEntity bloodInformation){
        bloodInformationService.updateById(bloodInformation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "血液信息", operate = "根据id删除血液信息")
    public R delete(@RequestBody Long[] ids){
        bloodInformationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 这个是实际用的
     */
    @ControllerLogAnnotation(module = "血液信息", operate = "根据bloodId级联删除血液入库和血液信息表数据")
    @PostMapping("/cascadeDelete")
    public R cascadeDelete(@RequestBody BloodDeleteReturnVo vo){
        int res= bloodInformationService.cascadeDelete(vo);
        return R.ok();
    }

    /**
     * 根据血液id数组修改为入库状态 同步更新血液信息表以及入库表
     * @param bloodids
     * @return
     */
    @RequestMapping("/batchUpdateInStockStateUsedBloodId")
    @ControllerLogAnnotation(module = "血液信息", operate = "根据id删除血液信息")
    public R batchUpdateInStockStateUsedBloodId(@RequestBody String[] bloodids){
        bloodInformationService.batchUpdateInStockStateByBloodId(bloodids);
        return R.ok();
    }
    /**
     * 根据血液id数组修改为退血状态 同步更新血液信息表以及入库表
     * @param bloodids
     * @return
     */
    @RequestMapping("/batchUpdateBloodReturnStateUsedBloodId")
    @ControllerLogAnnotation(module = "血液信息", operate = "根据血液id数组修改为退血状态 同步更新血液信息表以及入库表")
    public R batchUpdateBloodReturnStateUsedBloodId(@RequestBody String[] bloodids){
        bloodInformationService.batchUpdateBloodReturnStateByBloodId(bloodids);
        return R.ok();
    }

    /**
     * 根据血液id获取血袋的生命周期
     * @param bloodId
     * @return
     */
    @RequestMapping("/getBloodInformationIdHistory")
    @ControllerLogAnnotation(module = "血液信息", operate = "获取血袋的生命周期")
    public R getBloodInformationIdHistory(String bloodId){
        BloodInformationIdHistoryVo bloodInformationIdHistory = bloodInformationService.getBloodInformationIdHistory(bloodId);
        return R.ok().put("data", bloodInformationIdHistory);
    }

    /**
     * 获取血袋库存信息
     * @param
     * @return
     */
//    @RequestMapping("getBloodInventory")
//    @ControllerLogAnnotation(module = "血液信息", operate = "获取血袋库存信息")
//    public R getBloodInventory(){
//        Map<String, Map<String,String>> bloodInventoryInformation =bloodInformationService.getBloodInventory();
//        return R.ok().put("data",bloodInventoryInformation);
//    }

    @PostMapping("getBloodInventory")
    @ControllerLogAnnotation(module = "血液信息", operate = "获取血袋库存信息")
    public R getBloodInventory(@RequestBody(required = false) InventoryQueryVo inventoryQuery) {
        BloodInventoryResultVo bloodInventory = bloodInformationService.getBloodInventory(inventoryQuery);
        return R.ok().put("data", bloodInventory);
    }

    @PostMapping("/allSelectedIds")
    public R allSelectedIds(@RequestBody Set<String> bloodIdSet) {
        List<BloodInformationEntity> list = bloodInformationService.allSelectedIds(bloodIdSet);
        return R.ok().put("data", list);
    }


}
